package cn.tedu.sendmail;


import cn.tedu.util.IPUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.PrintWriter;
import java.io.StringWriter;

@RestController
public class MailController {



    @RequestMapping("/getIp")
    public String getIp(){

        return IPUtils.getIpAddr();
    }



    @Autowired
    JavaMailSender javaMailSender;

    public void sendMail(String subject,String text ) {
        //创建一封邮件
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        //设置收件人
        mailMessage.setTo("78546716@qq.com");
        //设置发件人
        mailMessage.setFrom("78546716@qq.com");
        //设置标题
        mailMessage.setSubject(subject);
        //设置正文
        mailMessage.setText(text);
        //发送邮件
        javaMailSender.send(mailMessage);
    }

    @RequestMapping("/send2")
    public String send2() {
        try {
            int n=10/0;
        } catch (Exception e) {
            //异常信息打印到服务器中，程序员看不到
            //e.printStackTrace();
            //把异常信息转成字符串
            StringWriter stingWriter=new StringWriter();
            PrintWriter printWriter=new PrintWriter(stingWriter);
            e.printStackTrace(printWriter);
            String execeptionInfo=stingWriter.toString();
            //return execeptionInfo;
            this.sendMail("异常信息", execeptionInfo);
            return "操作失败";
        }
        return "操作成功！";
    }




    @RequestMapping("/send1")
    public String send1() {
        sendMail("明天入职", "明天入职");


		return "操作成功！";
    }

}